//
//  SISSMyEnrollTenantViewController.m
//  SISSAPP
//
//  Created by 袁应荣 on 2017/12/19.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSMyEnrollTenantViewController.h"
#import "SISSCommonRentCell.h"
#import "SISSEnrollTenantDetailViewController.h"
#import "SISSNetworkingManager+Mine.h"

static NSString *myEnrollTenantCellReuseID = @"myEnrollTenantCellReuseID";
@interface SISSMyEnrollTenantViewController ()

@property (weak, nonatomic) IBOutlet SISSBaseTableView *tableView;

/** 房源数组 */
@property (copy, nonatomic) NSArray<SISSTenantModel *> *tenantArr;

@end

@implementation SISSMyEnrollTenantViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    
    WeakSelf
    [kSISSRefresh prepareHeaderRefresh:self.tableView loadDataBlock:^{
        kSISSRefresh.refreshType = SISSRefressTypeUpDown;
        [weakSelf upDownLoadData];
    }];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self requestMyEnrollTenantResourceData];
}

#pragma mark - UITableViewDelegate & UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    [tableView tableViewDisplayWithMessage:@"暂无数据" ifNecessaryForRowCount:self.tenantArr.count];
    return self.tenantArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SISSCommonRentCell *cell = [tableView dequeueReusableCellWithIdentifier:myEnrollTenantCellReuseID forIndexPath:indexPath];
    cell.isHideButton = YES;
    cell.isShowFullName = YES;
    cell.tenantModel = self.tenantArr[indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    SISSEnrollTenantDetailViewController *vc = [[SISSEnrollTenantDetailViewController alloc] init];
    vc.title = @"租客详情";
    vc.isChange = YES;
    vc.tenantModel = self.tenantArr[indexPath.row];
    vc.deleteComplete = ^{
        
        [self requestMyEnrollTenantResourceData];
    };
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - UI
- (void)setupSubviews {
    [super setupSubviews];
    
    // tableView相关
    // 自动行高
    self.tableView.estimatedRowHeight = 80;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentAutomatic;
    }
    // 去掉cell系统的线条
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    // 注册cell
    [self.tableView registerClass:[SISSCommonRentCell class] forCellReuseIdentifier:myEnrollTenantCellReuseID];
}

#pragma mark - 上下拉刷新加载方法
- (void)upDownLoadData {
    
    [self requestMyEnrollTenantResourceData];
}




#pragma mark - 6.3.4 获取登记的线下租客
- (void)requestMyEnrollTenantResourceData {
    
    WeakSelf
    [kNetworkManager requestOfflineTenantWithsuccess:^(NSArray<SISSTenantModel *> *tenantResources) {
       
        weakSelf.tenantArr = tenantResources;

        [[NSOperationQueue mainQueue] addOperationWithBlock:^{

            [weakSelf.tableView reloadData];

            [weakSelf.tableView.mj_header endRefreshing];
        }];
    } failed:^(NSError *error) {
        [weakSelf.tableView.mj_header endRefreshing];
        NSLog(@"%@",error);
    }];
}

@end
